<?php

namespace backend\controllers;

use app\models\ArcAndroid;
use app\models\ArcClassify;
use app\models\ArcProgress;
use app\models\ArcTrain;
use app\models\search\ThingRecordFileSearchModel;
use app\models\ThingRecordFile;
use common\helps\Arc;
use common\helps\ClassifySearch;
use common\helps\Tools;
use Yii;
use yii\bootstrap\Progress;
use yii\filters\VerbFilter;
use yii\web\NotFoundHttpException;
use yii\db\ActiveQuery;
use yii\data\Pagination;
use yii\helpers\Json;

/**
 * ThingRecordFileController implements the CRUD actions for ThingRecordFile model.
 */
class ThingRecordFileController extends CommonController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public function actionTest(){
        $res = Arc::sendAsrPost();
        var_dump($res);
    }

    /**
     * Lists all ThingRecordFile models.
     * @return mixed
     */
    public function actionIndex()
    {
        $post = Yii::$app->request->post();//拿到页面传来的时间值

        if (!empty($post) && $post != '') {
            Yii::$app->request->queryParams = ['ThingRecordFileSearchModel' => $post];
        }

        $searchModel = new ThingRecordFileSearchModel();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        $query = $dataProvider->query;
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count()]);
        if (isset($_POST['pageSize'])) {//设置了参数，就把参数给pageSize session
            setcookie('pageSize', $_POST['pageSize']);
        }
        if (!empty($_COOKIE['pageSize'])) {
            $pages->pageSize = (int)$_COOKIE['pageSize'];
        } else {
            $pages->pageSize = 5;
        }
        $dataProvider->pagination = $pages;
        $arcProgress = new ArcProgress();
        $newest = $arcProgress->find()->orderBy("id desc")->one();
        if (empty($newest)){
            $newestId = 0;
        }else{
            $newestId = $newest->thing_recordfile;
        }
        $newestOne = $countQuery->where("ID<=".$newestId)->count();
        $newestPage = (int)($newestOne/$pages->pageSize)+1;
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'url_base' => $this->url_base,
            'pages' => $pages,
            'newestPage' => $newestPage,
            'todayTotalCount' => $this->getTodayTotalCount(),
        ]);
    }

    /**
     * 返回今日总识别量.
     * @return string
     */
    public function getTodayTotalCount() {
        $start = strtotime(date('Y-m-d',time()));
        $end = $start + 86400;
        $start = strtotime(date('Y-m-d',time()));
        $end = $start + 86400;
        //有问题
        $problem = ArcProgress::find()->where(['>=','create_time',$start])->andWhere(['<','create_time',$end])->andWhere(['=','arc_classify_status',1])->count();
        //没问题
        $noproblem = ArcProgress::find()->where(['>=','create_time',$start])->andWhere(['<','create_time',$end])->andWhere(['=','arc_classify_status',2])->count();
        //今日识别总量
        $today['problem'] = $problem;
        $today['noproblem'] = $noproblem;
        return $today;
    }

    /**
     * Displays a single ThingRecordFile model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'url_base' => $this->url_base,
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * 该页面包含多个文件分类
     */
    public function actionClassifys()
    {
        return $this->render('classifys', []);
    }

    /**
     * 根据模型以及机器人来进行文件分类
     */
    public function actionClassify()
    {
        $this->layout = false;
        $request = Yii::$app->request;
        $arcTrainModel = new ArcTrain();
        $arcClassifyModel = new ArcClassify();
        $arcAndroidModel = new ArcAndroid();
        $arcProgressModel = new ArcProgress();
        $arcClassifys = $arcClassifyModel->find()->all();
        $arcAndroids = $arcAndroidModel->find()->all();
        if ($request->ispost) {
            //用到了以下四张表，当一次性完成时表示以及完成一次请求，可以提交事务
            $post = $request->post();
            if (!empty($post)) {
                /* 初始化 */
                $arcClassify = isset($post['arcClassify']) ? $post['arcClassify'] : '';//种类，模型
                $arcAndroid = isset($post['arcAndroid']) ? $post['arcAndroid'] : '';//机器人
                $startI = isset($post['startI']) ? $post['startI'] : '';//开始发送语音
                $arcTrainStart = isset($post['arcTrainStart']) ? $post['arcTrainStart'] : '';//开始进行训练，每个500次循环只能有一次的训练
                $continueTrain = isset($post['continueTrain']) ? $post['continueTrain'] : '';//为true表示的是继续训练
                if ($arcTrainStart != 'true') {//不是第一次才能接受
                    if (isset($_COOKIE['arcTrainId'])) {
                        $arcTrainId = $_COOKIE['arcTrainId'];
                    } else if (isset($post['arcTrainId'])) {
                        $arcTrainId = $post['arcTrainId'];
                    } else {
                        $arcTrainId = 0;
                    }
                    setcookie('arcTrainId', '');
                }
                /* 以下是开始进行机器人分类工作，1，首先添加一条训练表；2，然后添加500条进度表 */
                $arcTrainModel->arc_classify = $arcClassify;
                $arcTrainModel->arc_android = $arcAndroid;
                $sql = 'select count(*) from '
                    . $arcTrainModel->tableName() . ' where arc_android='
                    . $arcAndroid;//得到当前某个机器人已经使用的数量
                $number = 0;
                if ($arcTrainStart == 'true') {//第一次进入
                    $number = $arcTrainModel->getDb()->createCommand($sql)->queryScalar();//得到该机器人现在的总数量
                }
                $arc_android = $arcAndroidModel->findOne($arcAndroid);//得到该机器人
                $onetimeNum = $arc_android->onetime_num;//得到该机器人的一次可以训练的机器人数量，默认是500
                if ($number < $arc_android->number) {//数量比较小，小于规定的要求
                    //1，首先添加一条训练表
                    if ($arcTrainStart == 'true') {//如果传输的为分类种类以及机器人
                        $arcTrainModel->arc_android_num = $number + 1;
                        $arcTrainModel->progress = 1;
                        $arcTrainModel->create_time = time();
                        $arcTrainModel->user = Yii::$app->getUser()->id;
                        $arcTrainModel->ip = Tools::getip();
                        $arcTrainModel->save();//保存到了arc_train表
                        $arcTrainModel = $arcTrainModel->find()->where(['id' => $arcTrainModel->id])->one();
                    } else {//不是第一次
                        $arcTrainModel = $arcTrainModel->findOne($arcTrainId);
                        if ($continueTrain == 'true') {//表示的是继续训练,设置progress为1
                            $arcTrainModel->progress = 1;
                            $arcTrainModel->create_time = time();
                            $arcTrainModel->save();//保存到了arc_train表
                            $arcTrainModel = $arcTrainModel->findOne($arcTrainId);
                        }else if($continueTrain == 'false'){
                            //如果arcTrrain的progress为3表示的意思为该条记录已经被终止了，下面的不要走
                            if ($arcTrainModel->progress == 3) {
                                echo -5;
                                exit();
                            }
                        }
                    }
                    if (isset($arcTrainModel->id)) {
                        // 保存到了arc_train表成功
                        if ($startI < 0) {
                            $startI = 0;
                        }
                        if (0 <= $arcTrainModel->progress_num && $arcTrainModel->progress_num < $onetimeNum) {//进行默认500次的循环，每次都会发送本次拿到的语音文件到阿里云后台去进行识别
                            // 然后是将该进度表保存到arc_progress表
                            $thingRecordFileModel = new ThingRecordFile();
                            $newest = $arcProgressModel->find()->where('progress != 0')->orderBy('id desc')->one();
                            $newestId = isset($newest->thing_recordfile) ? $newest->thing_recordfile : 0;
                            //取最近的未训练的的录音文件，然后将该录音文件进行相应的识别分类
                            $thingRecordFile = $thingRecordFileModel->find()->where('ID > ' . $newestId)->one();
                            if (empty($thingRecordFile)) {
                                return -7;//表示已经训练完成所有的文件。
                                exit();
                            }
                            $parms = json_decode('' . str_replace('\'', '"', $arc_android->parms));
                            if (empty($parms)) {
                                return -6;
                                exit();
                            }
                            //本地语音文件的路径
                            $filePath = $thingRecordFile->Filepath;
                            //百度云下用getFilePath
                            //百度云下用getFilePathWindows
                            $filePath = Tools::getFilePath($filePath);
                            $httpFilePath = Tools::getUrlFilePath($filePath);
                            //linux下用./
                            $filePath1 = ".".$filePath;
                            //windows下用
                            $filePath2 = $filePath;
                            if (!file_exists($filePath1)){
                                Tools::httpcopy($httpFilePath,$filePath1);
                            }
                            if(file_exists($filePath1)) {
                                $parmsArr['progress'] = 2;
                            }else {
                                $parmsArr['progress'] = 4;
                            }
                            $parmsArr['thing_recordfile'] = $thingRecordFile->ID;
                            $parmsArr['arc_train'] = $arcTrainModel->id;
                            $parmsArr['arc_classify'] = (int)$arcClassify;
                            $parmsArr['arc_progress'] = isset($newest->id)? $newest->id:0;
                            $parmsArr['startI'] = (int)$startI;
                            $parmsArr['arcTrainId'] = $arcTrainModel->id;
                            $parmsArr['arcTrainProgressNum'] = $arcTrainModel->progress_num;
                            $parmsArr['onetimeNum'] = $onetimeNum;
                            //百度云的下用actionArcOneSyncBaidu
                            //阿里云的下用actionArcOneSyncAli
//                            $this->actionArcOneSyncBaidu($filePath,$parmsArr);
                            $this->actionArcOneSyncAli($filePath1,$parmsArr);
                        } else {
                            echo -1;//-1,最大的次数超过默认的500次或者onetimeNum次；
                            exit();
                        }
                    } else {
                        echo -2;//-2，单条训练数据保存失败
                        exit();
                    }
                } else {
                    echo -4;//-4,单个机器人不能创建太多的机器人副本
                    exit();
                }
            } else {
                setcookie('stopI', '');
                echo -5;//-5,主动关闭的训练
                exit();
            }
        }
        return $this->render('classify', [
            'arcTrainModel' => $arcTrainModel,
            'arcClassifys' => $arcClassifys,
            'arcAndroids' => $arcAndroids,
            'url_base' => $this->url_base,
            'arcTrainId' => isset($arcTrainModel->id) ? $arcTrainModel->id : 0,
        ]);
    }

    /**
     * 根据模型以及机器人来进行文件分类 中止或者停止
     */
    public function actionClassifyStop()
    {
        $arcProgressModel = ArcTrain::findOne($_POST['arcTrainId']);
        $arcProgressModel->updateAttributes(['progress'=>3]);//3中止
        return -5;exit();
    }

    /**
     */
    public function actionRefresh()
    {
        try{
            $arcTrainId = $_POST['arcTrainId'];
            $arcTrain = ArcTrain::findOne($arcTrainId);
            $arcAndroid = ArcAndroid::findOne($arcTrain->arc_android);
            $res = [];
            $res['arcTrainId'] = $arcTrainId;
            $res['arcTrainProgressNum'] = $arcTrain->progress_num;
            $res['onetimeNum'] = $arcAndroid->onetime_num;
            echo json_encode($res);
            exit();
        }catch (\Exception $exception){
            var_dump($exception);
            exit();
        }
    }

    /**
     * 根据模型以及机器人来进行文件分类 中止或者停止
     */
    public function actionClassifyStop1()
    {
        $request = Yii::$app->request;
        if ($request->ispost) {
            $post = $request->post();
            $arcTrainId = isset($post['arcTrainId']) ? $post['arcTrainId'] : 0;
            $arcTrainModel = ArcTrain::findOne($arcTrainId);
            $arcTrainModel->progress = 3;
            $res = $arcTrainModel->update();
            if ($res) {
                echo 1;
            } else {
                echo -1;
            }
            exit();
        }elseif ($request->isGet) {
            $arcTrainId = $request->get('arcTrainId',0);
            $arcTrainModel = ArcTrain::findOne($arcTrainId);
            $arcTrainModel->progress = 3;
            $res = $arcTrainModel->update();
            if ($res) {
                return 1;
            } else {
                return -1;
            }
        }
    }

    /**
     * 根据模型以及机器人种类来进行相应的继续文件分类等操作
     */
    public function actionClassifyContinue()
    {
        $this->layout = false;
        $request = Yii::$app->request;
        $arcTrainModel = new ArcTrain();
        $arcClassifyModel = new ArcClassify();
        $arcAndroidModel = new ArcAndroid();
        $arcProgressModel = new ArcProgress();
        $arcClassifys = $arcClassifyModel->find()->all();
        $arcAndroids = $arcAndroidModel->find()->all();
        if ($request->ispost) {
            //用到了以下四张表，当一次性完成时表示以及完成一次请求，可以提交事务
            $post = $request->post();
            if (!empty($post)) {
                /* 初始化 */
                $arcClassify = isset($post['arcClassify']) ? $post['arcClassify'] : '';//种类，模型
                $arcAndroid = isset($post['arcAndroid']) ? $post['arcAndroid'] : '';//机器人
                $startI = isset($post['startI']) ? $post['startI'] : '';//开始发送语音
                $arcTrainStart = isset($post['arcTrainStart']) ? $post['arcTrainStart'] : '';//开始进行训练，每个500次循环只能有一次的训练
                $continueTrain = isset($post['continueTrain']) ? $post['continueTrain'] : '';//为true表示的是继续训练
                if ($arcTrainStart != 'true') {//不是第一次才能接受
                    if (isset($_COOKIE['arcTrainId'])) {
                        $arcTrainId = $_COOKIE['arcTrainId'];
                    } else if (isset($post['arcTrainId'])) {
                        $arcTrainId = $post['arcTrainId'];
                    } else {
                        $arcTrainId = 0;
                    }
                    setcookie('arcTrainId', '');
                }
                /* 以下是开始进行机器人分类工作，1，首先添加一条训练表；2，然后添加500条进度表 */
                $arcTrainModel->arc_classify = $arcClassify;
                $arcTrainModel->arc_android = $arcAndroid;
                $sql = 'select count(*) from '
                    . $arcTrainModel->tableName() . ' where arc_android='
                    . $arcAndroid;//得到当前某个机器人已经使用的数量
                    $number = $arcTrainModel->getDb()->createCommand($sql)->queryScalar();//得到该机器人现在的总数量
                $arc_android = $arcAndroidModel->findOne($arcAndroid);//得到该机器人
                $onetimeNum = $arc_android->onetime_num;//得到该机器人的一次可以训练的机器人数量，默认是500
//                if ($number < $arc_android->number) {//数量比较小，小于规定的要求
                    //1，首先添加一条训练表
                    $arcTrainModel = $arcTrainModel->findOne($arcTrainId);
                    if ($continueTrain == 'true') {//表示的是继续训练,设置progress为1
                        $arcTrainModel->progress = 1;
                        $arcTrainModel->save();//保存到了arc_train表
                        $arcTrainModel = $arcTrainModel->findOne($arcTrainId);
                    }else if($continueTrain == 'false'){
                        //如果arcTrrain的progress为3表示的意思为该条记录已经被终止了，下面的不要走
                        if ($arcTrainModel->progress == 3) {
                            echo -5;
                            exit();
                        }
                    }
                    if (isset($arcTrainModel->id)) {
                        // 保存到了arc_train表成功
                        if ($startI < 0) {
                            $startI = 0;
                        }
                        if (0 <= $arcTrainModel->progress_num && $arcTrainModel->progress_num < $onetimeNum) {//进行默认500次的循环，每次都会发送本次拿到的语音文件到阿里云后台去进行识别
                            // 然后是将该进度表保存到arc_progress表
                            $thingRecordFileModel = new ThingRecordFile();
                            $newest = $arcProgressModel->find()->where('progress != 0')->orderBy('id desc')->one();
                            $newestId = isset($newest->thing_recordfile) ? $newest->thing_recordfile : 0;
                            //取最近的未训练的的录音文件，然后将该录音文件进行相应的识别分类
                            $thingRecordFile = $thingRecordFileModel->find()->where('ID > ' . $newestId)->one();
                            if (empty($thingRecordFile)) {
                                return -7;//表示已经训练完成所有的文件。
                                exit();
                            }
                            $parms = json_decode('' . str_replace('\'', '"', $arc_android->parms));
                            if (empty($parms)) {
                                return -6;
                                exit();
                            }
                            //本地语音文件的路径
                            $filePath = $thingRecordFile->Filepath;
                            //linux下用getFilePath
                            //windows系统下用getFilePathWindows
                            $filePath = Tools::getFilePath($filePath);
                            $httpFilePath = Tools::getUrlFilePath($filePath);
                            //linux下用./
                            $filePath1 = ".".$filePath;
                            //windows下用
                            $filePath2 = $filePath;
                            if (!file_exists($filePath1)){
                                Tools::httpcopy($httpFilePath,$filePath1);
                            }
                            if(file_exists($filePath1)) {
                                $parmsArr['progress'] = 2;
                            }else {
                                $parmsArr['progress'] = 4;
                            }
                            $parmsArr['thing_recordfile'] = $thingRecordFile->ID;
                            $parmsArr['arc_train'] = $arcTrainModel->id;
                            $parmsArr['arc_classify'] = (int)$arcClassify;
                            $parmsArr['arc_progress'] = isset($newest->id)? $newest->id:0;
                            $parmsArr['startI'] = (int)$startI;
                            //百度云下用actionArcOneSync
                            //阿里云下用actionArcOneSyncWindows
//                            $this->actionArcOneSyncBaidu($filePath,$parmsArr);
                            $this->actionArcOneSyncAliContinue($filePath1,$parmsArr);
                        } else {
                            echo -1;//-1,最大的次数超过默认的500次或者onetimeNum次；
                            exit();
                        }
                    } else {
                        echo -2;//-2，单条训练数据保存失败
                        exit();
                    }
//                } else {
//                    echo -4;//-4,单个机器人不能创建太多的机器人副本
//                    exit();
//                }
            } else {
                setcookie('stopI', '');
                echo -5;//-5,主动关闭的训练
                exit();
            }
        } else {
            $arcTrainId = isset($_GET['arcTrainId']) ? $_GET['arcTrainId'] : 0;
            $arcTrainModel = $arcTrainModel->findOne($arcTrainId);
            $arc_android = $arcAndroidModel->findOne($arcTrainModel->arc_android);//得到该机器人
            $maxNumber = $arc_android->number;//得到该机器人的最大数量默认为10
            $onetimeNum = $arc_android->onetime_num;//得到该机器人的一次可以训练的机器人数量，默认是500
            $progress_num = $arcTrainModel->progress_num;
//            if($progress_num>=$onetimeNum){
//                echo -7;
//                exit();
//            }
        }
        return $this->render('classify-continue', [
            'arcTrainModel' => $arcTrainModel,
            'arcClassifys' => $arcClassifys,
            'arcAndroids' => $arcAndroids,
            'maxNumber' => $maxNumber,
            'progress_num' => $progress_num,
            'onetimeNum' => $onetimeNum,
            'url_base' => $this->url_base,
            'arcTrainId' => isset($arcTrainModel->id) ? $arcTrainModel->id : 0,
        ]);
    }
    /**
     * 根据模型以及机器人种类来进行相应的继续文件分类等操作
     */
    public function actionClassifyContinue1()
    {
        $this->layout = false;
        $request = Yii::$app->request;
        $arcTrainModel = new ArcTrain();
        $arcClassifyModel = new ArcClassify();
        $arcAndroidModel = new ArcAndroid();
        $arcProgressModel = new ArcProgress();
        $arcClassifys = $arcClassifyModel->find()->all();
        $arcAndroids = $arcAndroidModel->find()->all();
        if ($request->ispost) {
            //用到了以下四张表，当一次性完成时表示以及完成一次请求，可以提交事务
            $post = $request->post();
            if (!empty($post)) {
                /* 初始化 */
                $arcClassify = isset($post['arcClassify']) ? $post['arcClassify'] : '';//种类，模型
                $arcAndroid = isset($post['arcAndroid']) ? $post['arcAndroid'] : '';//机器人
                $startI = isset($post['startI']) ? $post['startI'] : '';//开始发送语音
                $arcTrainStart = isset($post['arcTrainStart']) ? $post['arcTrainStart'] : '';//开始进行训练，每个500次循环只能有一次的训练
                $continueTrain = isset($post['continueTrain']) ? $post['continueTrain'] : '';//为true表示的是继续训练
                if ($arcTrainStart != 'true') {//不是第一次才能接受
                    if (isset($_COOKIE['arcTrainId'])) {
                        $arcTrainId = $_COOKIE['arcTrainId'];
                    } else if (isset($post['arcTrainId'])) {
                        $arcTrainId = $post['arcTrainId'];
                    } else {
                        $arcTrainId = 0;
                    }
                    setcookie('arcTrainId', '');
                }
                /* 以下是开始进行机器人分类工作，1，首先添加一条训练表；2，然后添加500条进度表 */
                $arcTrainModel->arc_classify = $arcClassify;
                $arcTrainModel->arc_android = $arcAndroid;
                $sql = 'select count(*) from '
                    . $arcTrainModel->tableName() . ' where arc_android='
                    . $arcAndroid;//得到当前某个机器人已经使用的数量
                $number = $arcTrainModel->getDb()->createCommand($sql)->queryScalar();//得到该机器人现在的总数量
                $arc_android = $arcAndroidModel->findOne($arcAndroid);//得到该机器人
                $onetimeNum = $arc_android->onetime_num;//得到该机器人的一次可以训练的机器人数量，默认是500
//                if ($number < $arc_android->number) {//数量比较小，小于规定的要求
                //1，首先添加一条训练表
                $arcTrainModel = $arcTrainModel->findOne($arcTrainId);
                if ($continueTrain == 'true') {//表示的是继续训练,设置progress为1
                    $arcTrainModel->progress = 1;
                    $arcTrainModel->save();//保存到了arc_train表
                    $arcTrainModel = $arcTrainModel->findOne($arcTrainId);
                }else if($continueTrain == 'false'){
                    //如果arcTrrain的progress为3表示的意思为该条记录已经被终止了，下面的不要走
                    if ($arcTrainModel->progress == 3) {
                        echo -5;
                        exit();
                    }
                }
                if (isset($arcTrainModel->id)) {
                    // 保存到了arc_train表成功
                    if ($startI < 0) {
                        $startI = 0;
                    }
                    if (0 <= $arcTrainModel->progress_num && $arcTrainModel->progress_num < $onetimeNum) {//进行默认500次的循环，每次都会发送本次拿到的语音文件到阿里云后台去进行识别
                        // 然后是将该进度表保存到arc_progress表
                        $thingRecordFileModel = new ThingRecordFile();
                        $newest = $arcProgressModel->find()->where('progress != 0')->orderBy('id desc')->one();
                        $newestId = isset($newest->thing_recordfile) ? $newest->thing_recordfile : 0;
                        //取最近的未训练的的录音文件，然后将该录音文件进行相应的识别分类
                        $thingRecordFile = $thingRecordFileModel->find()->where('ID > ' . $newestId)->one();
                        if (empty($thingRecordFile)) {
                            return -7;//表示已经训练完成所有的文件。
                            exit();
                        }
                        $parms = json_decode('' . str_replace('\'', '"', $arc_android->parms));
                        if (empty($parms)) {
                            return -6;
                            exit();
                        }
                        //本地语音文件的路径
                        $filePath = $thingRecordFile->Filepath;
                        //linux下用getFilePath
                        //windows系统下用getFilePathWindows
                        $filePath = Tools::getFilePath($filePath);
                        $httpFilePath = Tools::getUrlFilePath($filePath);
                        //linux下用./
                        $filePath1 = ".".$filePath;
                        //windows下用
                        $filePath2 = $filePath;
                        if (!file_exists($filePath1)){
                            Tools::httpcopy($httpFilePath,$filePath1);
                        }
                        if(file_exists($filePath1)) {
                            $parmsArr['progress'] = 2;
                        }else {
                            $parmsArr['progress'] = 4;
                        }
                        $parmsArr['thing_recordfile'] = $thingRecordFile->ID;
                        $parmsArr['arc_train'] = $arcTrainModel->id;
                        $parmsArr['arc_classify'] = (int)$arcClassify;
                        $parmsArr['arc_progress'] = $newest->id;
                        $parmsArr['startI'] = (int)$startI;
                        //百度云下用actionArcOneSync
                        //阿里云下用actionArcOneSyncWindows
//                            $this->actionArcOneSyncBaidu($filePath,$parmsArr);
                        $this->actionArcOneSyncAli($filePath1,$parmsArr);
                    } else {
                        echo -1;//-1,最大的次数超过默认的500次或者onetimeNum次；
                        exit();
                    }
                } else {
                    echo -2;//-2，单条训练数据保存失败
                    exit();
                }
//                } else {
//                    echo -4;//-4,单个机器人不能创建太多的机器人副本
//                    exit();
//                }
            } else {
                setcookie('stopI', '');
                echo -5;//-5,主动关闭的训练
                exit();
            }
        } else {
            $arcTrainId = isset($_GET['arcTrainId']) ? $_GET['arcTrainId'] : 0;
            $arcTrainModel = $arcTrainModel->findOne($arcTrainId);
            $arc_android = $arcAndroidModel->findOne($arcTrainModel->arc_android);//得到该机器人
            $maxNumber = $arc_android->number;//得到该机器人的最大数量默认为10
            $onetimeNum = $arc_android->onetime_num;//得到该机器人的一次可以训练的机器人数量，默认是500
            $progress_num = $arcTrainModel->progress_num;
//            if($progress_num>=$onetimeNum){
//                echo -7;
//                exit();
//            }
        }
        return $this->render('classify-continue', [
            'arcTrainModel' => $arcTrainModel,
            'arcClassifys' => $arcClassifys,
            'arcAndroids' => $arcAndroids,
            'maxNumber' => $maxNumber,
            'progress_num' => $progress_num,
            'onetimeNum' => $onetimeNum,
            'url_base' => $this->url_base,
            'arcTrainId' => isset($arcTrainModel->id) ? $arcTrainModel->id : 0,
        ]);
    }

    /**
     * Creates a new ThingRecordFile model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new ThingRecordFile();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->ID]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing ThingRecordFile model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->ID]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing ThingRecordFile model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the ThingRecordFile model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ThingRecordFile the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ThingRecordFile::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
